#coding:utf-8
from flask import Flask
from flask import request,jsonify
import json
import sys
reload(sys)
sys.setdefaultencoding('utf8')
app = Flask(__name__)
from flask import render_template
from data.CodeStaticsData import *
@app.route('/')
def index():
    return render_template('codeStatics.html')

@app.route('/query', methods= ["POST"])
def singleQuery():
    paramData = request.values.to_dict()
    module = paramData["module"]
    month = paramData["month"]
    # print "module: " +module + " month: " +month
    # print type(module)
    dicStr = getData(module,month)
    dicArr = []
    dicArr.append(dicStr)
    # print "dic"
    # print dicStr
    return jsonify(dicArr)

@app.route('/moduleBatchquery', methods= ["POST"])
def moduleBatchquery():
    # paramData = request.values.to_dict()
    # print paramData
    paramData = request.get_json()
    moduleArr = paramData["module"]
    month = paramData["month"]
    dicArr = []
    for module in moduleArr:
        dicStr = getData(module,month)
        dicArr.append(dicStr)

    return jsonify(dicArr)

def getData(module,month):
    codeLineNum = codeStatics(module,month)

    if codeLineNum == None:
        codeLineNum = 0
        print "Get codeLineNum fail"
        # return None
    bugNum = getBugNum(module, month)
    dic = {}
    dic["module"] = module
    dic["month"] = month
    dic["codeLineNum"] = codeLineNum
    dic["bugNum"] = bugNum
    dic["bugRate"] = round(bugNum/float(codeLineNum),3)
    dicStr = json.dumps(dic)
    return dicStr


if __name__ == '__main__':
    app.run(host="172.18.220.26",port=5000)
